package com.example.kotlin.lifecycle

import java.util.*

/**
 * @ClassName: FragmentLifeCycle
 * @Description: 被观察者
 * @Author: chenmengjia
 * @Date: 2022/2/3 9:51
 */
class FragmentLifecycle : Lifecycle {
    private val list: MutableList<LifecycleObserver?> = ArrayList()
    override fun addListener(lifeCycleListener: LifecycleObserver?) {
        list.add(lifeCycleListener)
    }

    override fun removeListener(lifeCycleListener: LifecycleObserver?) {
        list.remove(lifeCycleListener)
    }

    fun onStart() {
        for (lifecycleListener in list) {
            lifecycleListener?.onStart()
        }
    }

    fun onDestroy() {
        for (lifecycleListener in list) {
            lifecycleListener?.onDestroy()
        }
    }
}